$(function () {
    local()
    let page = 1
    let pageSize = 6
    let str = ``
    let goods = []
    //初始加载
    mui.init({
        pullRefresh: {
            container: "#refreshContainer",//下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
            up: {
                height: 50,//可选,默认50.触发下拉刷新拖动距离,
                auto: true,
                contentrefresh: "正在刷新...",//可选，正在刷新状态时，下拉刷新控件上显示的标题内容
                contextnomore: '没有更多的商品了...',
                callback: downFlesh //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
            }
        }
    })

    //下拉加载商品
    function downFlesh() {
        let that = this
        $.ajax({
            type: 'GET',
            url: '/cart/queryCartPaging',
            data: {
                page: page++,
                pageSize
            },
            success: (result) => {
                if (result) {
                    if (result.error) {
                        mui.toast(result.message)
                    } else {
                        if (result.data?.length > 0) {
                            initUpdate(result)
                            that.endPullupToRefresh(false)
                        } else {
                            that.endPullupToRefresh(true)
                        }

                    }
                } else {
                    mui.toast('无法访问服务,请联系wx:13278865643')
                    getLocalHistory()
                }
            }
        })
    }

    //加载购物车列表
    function initUpdate(shoppings) {
        goods = shoppings.data
        for (let g in goods) {
            str += `
                    <li class="mui-table-view-cell mui-media" id="updateShop" data-id="${goods[g].id}">
                        <img class="mui-media-object mui-pull-left" src="${goods[g].pic[0].picAddr}">
                            <div class="mui-media-body">
                                <p class="mui-ellipsis">${goods[g].proName}</p>
                                     <p><span class="span1">尺码:${goods[g].size}</span><span class="span1">数量:${goods[g].num}</span><span
                                            class="span1">价格:${parseInt(goods[g].price * goods[g].num)}元</span></p>
                                    <p><button class="mui-pull-right mui-btn-danger" id="btn_del" data-id="${goods[g].id}"><span>删除</span></button><button
                                            class="mui-pull-right mui-btn-warning" id="btn_buy" data-id="${goods[g].id}"><span>确认购买</span></button></p>
                                </div>
                            </li>        
                        `
            $('.ul_goods').html(str)
        }
    }

    //删除购物车
    $(document).on('tap', '#btn_del', (e) => {
        let id = e.currentTarget.dataset.id
        $.ajax({
            type: 'GET',
            url: '/cart/deleteCart',
            data: {
                id
            },
            success: (result) => {
                if (result.success) {
                    mui.toast('删除成功')
                    window.location.replace('/shoppingCar')
                } else {
                    mui.toast('网络不佳!')
                }
            }
        })
    })

    //点击购买功能
    $(document).on('tap', '#btn_buy', (e) => {
        let id = e.currentTarget.dataset.id
        $.ajax({
            type: 'GET',
            url: '/cart/deleteCart',
            data: {
                id
            },
            success: (result) => {
                if (result.success) {
                    mui.alert('购买成功!', () => {
                        window.location.replace('/shoppingCar')
                    })
                } else {
                    mui.toast('网络不佳!')
                }
            }
        })
    })

    //点击商品进行前往修改
    $(document).on('tap', '#updateShop', (e) => {
        for (let g in goods) {
            if (e.currentTarget.dataset.id == goods[g].id) {
                localStorage.setItem('goodUpdate', JSON.stringify(goods[g]))
            }
            window.location.href = '/shoppingUpdate'
        }
    })
})
